package com.zzf.concurrent.example.design.pattern.thread.local;

import java.util.HashMap;

/**
 * @author GaoFeng2017
 * @date 2019/9/8 13:37
 */
public class SimpleThreadLocal<T> {
    private HashMap<String, T> threadDataMap = new HashMap<>();

    public void set(T data) {
        synchronized (SimpleThreadLocal.class) {
            String key = Thread.currentThread().getName();
            threadDataMap.put(key, data);
        }
    }

    public T get() {
        synchronized (SimpleThreadLocal.class) {
            String key = Thread.currentThread().getName();
            T result = threadDataMap.get(key);
            return result == null ? this.initialValue() : result;
        }
    }

    protected T initialValue() {
        return null;
    }
}
